<template>
  <div>
    <el-form :inline="true" :rules="rules" ref="ruleForm" :model="user" class="demo-form-inline">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="user.username" placeholder="用户名"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="user.password" placeholder="密码" show-password></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login('ruleForm')">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "LoginView",
  data() {
    return {
      user:{},
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'}
        ],
      }
    }
  }, methods: {

    login(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios.post("auth/login",this.user).then(res=>{
            if (res.data.code==200){
              this.$message.success(res.data.msg);
              sessionStorage.getItem("token",res.data.user);
              this.$router.push("/list");
            }else {
              this.$message.error(res.data.msg);
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }, created() {
  }
}
</script>

<style scoped>

</style>